//
//  AccountInfoView.m
//  CedarFinancePH
//
//  Created by menchao on 2018/6/19.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "AccountInfoView.h"
#include <UIView+TYAlertView.h>

@interface AccountInfoView()

@property (nonatomic,copy) NSString *phone;
@property (nonatomic,weak) UITextField *inputTextField;
@property (nonatomic,weak) ProductPayInfoRsp *productPayInfo;

@property (nonatomic,strong) UILabel *timeTipsLbl;

@end


@implementation AccountInfoView


- (instancetype)initWithProductPayInfo:(ProductPayInfoRsp *)productPayInfo{
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        self.productPayInfo = productPayInfo;
        self.layer.cornerRadius = 12.0;
        self.layer.masksToBounds = YES;
        if(self.productPayInfo.showStatus == 1){
            self.frame = CGRectMake(0, 0, 322, 260);
            [self setupSubviews];
        }else if(productPayInfo.showStatus == 0){
            self.frame = CGRectMake(0, 0, 322, 180);
            [self setupSubviews2];
        }
    }
    return self;
}

- (void)showNotPaymentBooking:(ProductPayInfoRsp * )productPayInfo{
    if (productPayInfo){
        NSString *timeStr;
        double minute = [productPayInfo.minute doubleValue];
        NSString *startDateTime = [NSString stringWithFormat:@"%@年%@月%@日%@",productPayInfo.showAccountYear,productPayInfo.showAccountMonth,productPayInfo.showAccountDay,productPayInfo.showAccountTime];
        NSString *endDateTime ;
        if (minute >0) {
            endDateTime = [NSString stringWithFormat:@"%@年%@月%@日%@时%@分",productPayInfo.payYear,productPayInfo.payMonth,productPayInfo.payDay,productPayInfo.payHour,productPayInfo.minute];
        }else{
            endDateTime = [NSString stringWithFormat:@"%@年%@月%@日%@时",productPayInfo.payYear,productPayInfo.payMonth,productPayInfo.payDay,productPayInfo.payHour];
        }
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:5];//行间距
        NSDictionary *attributes = @{NSParagraphStyleAttributeName:paragraphStyle};
        timeStr = [NSString stringWithFormat:@"打款账户信息将于%@公布，请提前准备好资金，并于%@前完成打款。",startDateTime,endDateTime];
        NSMutableAttributedString *timeAttributedStr = [[NSMutableAttributedString alloc]initWithString:timeStr attributes:attributes];
        NSDictionary *attrDict = @{NSForegroundColorAttributeName:kBlueColor};
        
        //头部查找
        NSRange range0 =  [CedarTools findString:[timeAttributedStr string] start:@"将于" end:@"年"];
        [timeAttributedStr addAttributes:attrDict range:range0];
        NSRange range1 =  [CedarTools findString:[timeAttributedStr string] start:@"年" end:@"月"];
        [timeAttributedStr addAttributes:attrDict range:range1];
        NSRange range2 =  [CedarTools findString:[timeAttributedStr string] start:@"月" end:@"日"];
        [timeAttributedStr addAttributes:attrDict range:range2];
        NSRange range3 =  [CedarTools findString:[timeAttributedStr string] start:@"日" end:@"公布"];
        [timeAttributedStr addAttributes:attrDict range:range3];
        NSRange range4 =  [CedarTools findString:[timeAttributedStr string] start:@"可以在" end:@"页面查看"];
        [timeAttributedStr addAttributes:attrDict range:range4];
        
        //尾部先查找
        NSRange range5 =  [CedarTools findStringBackwards:[timeAttributedStr string] start:@"并于" end:@"年"];
        [timeAttributedStr addAttributes:attrDict range:range5];
        NSRange range6 =  [CedarTools findStringBackwards:[timeAttributedStr string] start:@"年" end:@"月"];
        [timeAttributedStr addAttributes:attrDict range:range6];
        NSRange range7 =  [CedarTools findStringBackwards:[timeAttributedStr string] start:@"月" end:@"日"];
        [timeAttributedStr addAttributes:attrDict range:range7];
        NSRange range8 =  [CedarTools findStringBackwards:[timeAttributedStr string] start:@"日" end:@"时"];
        [timeAttributedStr addAttributes:attrDict range:range8];
        self.timeTipsLbl.attributedText = timeAttributedStr;
     }
}


- (void)setupSubviews2{
    UILabel *titleLbl = [[UILabel alloc]init];
    titleLbl.textColor = kBlackColor;
    titleLbl.text = @"产品打款账户信息";
    titleLbl.font = [UIConfigure  configureAPPMediumFont:16];
    [self addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.equalTo(self).mas_offset(15);
    }];
    
    UILabel *timeLbl = [[UILabel alloc]init];
    timeLbl.lineBreakMode = NSLineBreakByWordWrapping;
    timeLbl.numberOfLines = 0;
    MACRO_UILabel_Configure(timeLbl, @"请于年月日点前打款到如下指定账户", kBlackColor, 13)
    self.timeTipsLbl = timeLbl;
    [self addSubview:timeLbl];
    [timeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).mas_offset(14);
        make.right.mas_equalTo(self).mas_offset(-14);
        make.top.equalTo(titleLbl.mas_bottom).mas_offset(6);
    }];
    
    CedarButton *cancelBtn =  [CedarButton buttonWithRegisterText:@"我知道了"];
    @weakify(self)
    cancelBtn.clickEvent = ^{
        @strongify(self)
        [self hideView];
    };
    [self addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).mas_offset(55);
        make.right.mas_equalTo(self).mas_offset(-55);
        make.bottom.mas_equalTo(self).mas_offset(-15);
        make.height.mas_offset(40);
    }];
    [self showNotPaymentBooking:self.productPayInfo];
}




- (void)setupSubviews{
    UILabel *titleLbl = [[UILabel alloc]init];
    titleLbl.textColor = kBlackColor;
    titleLbl.text = @"产品打款账户信息";
    titleLbl.font = [UIConfigure  configureAPPMediumFont:16];
    [self addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.equalTo(self).offset(15);
    }];
  
    UILabel *timeLbl = [[UILabel alloc]init];
    timeLbl.lineBreakMode = NSLineBreakByWordWrapping;
    timeLbl.numberOfLines = 0;
    MACRO_UILabel_Configure(timeLbl, @"请于XXXX年XX月X日XX点前打款到如下指定账户", kBlackColor, 13)
    self.timeTipsLbl = timeLbl;
    [self addSubview:timeLbl];
    [timeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(14);
        make.right.mas_equalTo(self);
        make.top.equalTo(titleLbl.mas_bottom).offset(5);
    }];
    
    UILabel *accountNameTitleLbl = [[UILabel alloc]init];
    MACRO_UILabel_Configure(accountNameTitleLbl, @"账户名称:", kMasterColor, 13);
    [self addSubview:accountNameTitleLbl];
    [accountNameTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.top.equalTo(timeLbl.mas_bottom).offset(10);
    }];
    UILabel *accountNameLbl = [[UILabel alloc]init];
    NSString *accountName = self.productPayInfo.accountName;
    AvoidNullString(accountName);
    MACRO_UILabel_Configure(accountNameLbl,accountName, kMasterColor, 13);
    [self addSubview:accountNameLbl];
    [accountNameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(accountNameTitleLbl);
        make.left.mas_equalTo(accountNameTitleLbl.mas_right).offset(5);
    }];
    
    
    UILabel *accountCardTitleLbl = [[UILabel alloc]init];
    MACRO_UILabel_Configure(accountCardTitleLbl, @"账  号:", kMasterColor, 13);
    [self addSubview:accountCardTitleLbl];
    [accountCardTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(accountNameTitleLbl);
        make.top.mas_equalTo(accountNameTitleLbl.mas_bottom).offset(3);
    }];
    UILabel *accountCardLbl = [[UILabel alloc]init];
    NSString *accountNo = self.productPayInfo.accountNo;
    AvoidNullString(accountName);
    MACRO_UILabel_Configure(accountCardLbl, accountNo, kMasterColor, 13);
    [self addSubview:accountCardLbl];
    [accountCardLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(accountCardTitleLbl);
        make.left.mas_equalTo(accountNameLbl);
    }];
    
    UILabel *bankTitleLbl = [[UILabel alloc]init];
    MACRO_UILabel_Configure(bankTitleLbl, @"开户银行:", kMasterColor, 13);
    [self addSubview:bankTitleLbl];
    [bankTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(accountNameTitleLbl);
        make.top.mas_equalTo(accountCardTitleLbl.mas_bottom).offset(3);
    }];
    UILabel *bankLbl = [[UILabel alloc]init];
    NSString *accountBank = self.productPayInfo.accountBank;
    AvoidNullString(accountName);
    MACRO_UILabel_Configure(bankLbl, accountBank, kMasterColor, 14);
    [self addSubview:bankLbl];
    [bankLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(bankTitleLbl);
        make.left.mas_equalTo(accountNameLbl);
    }];
    
    UILabel *tipsLbl = [[UILabel alloc]init];
    MACRO_UILabel_Configure(tipsLbl, @"如在规定时间内未收到您的打款,您的预约将被视为无效。",kGrayColor , 13);
    tipsLbl.numberOfLines = 0;
    [self addSubview:tipsLbl];
    [tipsLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(accountNameTitleLbl);
        make.right.mas_equalTo(self).mas_offset(-5);
        make.top.mas_equalTo(bankTitleLbl.mas_bottom).offset(8);
    }];
    
    UILabel *warningLbl = [[UILabel alloc]init];
    MACRO_UILabel_Configure(warningLbl, @"请务必使用绑定的银行卡进行线下打款!",[UIColor redColor] , 13);
    [self addSubview:warningLbl];
    [warningLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(accountNameTitleLbl);
        make.top.mas_equalTo(tipsLbl.mas_bottom).offset(3);
    }];
    
    CedarButton *cancelBtn =  [CedarButton buttonWithRegisterText:@"我知道了"];
    @weakify(self)
    cancelBtn.clickEvent = ^{
        @strongify(self)
        [self hideView];
    };
    [self addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).mas_offset(55);
        make.right.equalTo(self).mas_offset(-55);
        make.top.equalTo(warningLbl.mas_bottom).mas_offset(10);
        make.height.mas_offset(40);
    }];
    
    [self updateTime:self.productPayInfo];
}

- (void)updateTime:(ProductPayInfoRsp *)productPayInfo{
    if (productPayInfo) {
        NSString *timeStr;
        double minute = [productPayInfo.minute doubleValue];
        if (minute <= 0) {
            if(IsEmptyStr(productPayInfo.payHour)){
                productPayInfo.payHour = @"24";
            }
            timeStr = [NSString stringWithFormat:@"请于%@年%@月%@日%@点前打款到如下指定账户:",productPayInfo.payYear,productPayInfo.payMonth,productPayInfo.payDay,productPayInfo.payHour];
        }else{
            timeStr = [NSString stringWithFormat:@"请于%@年%@月%@日%@点%@分前打款到如下指定账户:",productPayInfo.payYear,productPayInfo.payMonth,productPayInfo.payDay,productPayInfo.payHour,productPayInfo.minute];
        }
        NSMutableAttributedString *timeAttributedStr = [[NSMutableAttributedString alloc] initWithString:timeStr];
        NSDictionary *attrDict = @{NSForegroundColorAttributeName:kBlueColor};
        //找年
        NSRange range0 =  [CedarTools findString:[timeAttributedStr string] start:@"请于" end:@"年"];
        [timeAttributedStr addAttributes:attrDict range:range0];
        
        NSRange range1 =  [CedarTools findString:[timeAttributedStr string] start:@"年" end:@"月"];
        [timeAttributedStr addAttributes:attrDict range:range1];
        
        NSRange range2 =  [CedarTools findString:[timeAttributedStr string] start:@"月" end:@"日"];
        [timeAttributedStr addAttributes:attrDict range:range2];
        
        NSRange range3 =  [CedarTools findString:[timeAttributedStr string] start:@"日" end:@"点"];
        [timeAttributedStr addAttributes:attrDict range:range3];
        if (minute >0){
            NSRange range4 =  [CedarTools findString:[timeAttributedStr string] start:@"点" end:@"分"];
            [timeAttributedStr addAttributes:attrDict range:range4];
        }
        self.timeTipsLbl.attributedText = timeAttributedStr;
    }

}



@end
